package com.zfei.gfb.controller;

import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Lists;
import com.google.gson.JsonObject;
import com.zfei.gfb.api.StatsService;
import com.zfei.gfb.api.domain.StatsTag;
import com.zfei.gfb.bean.Const;
import com.zfei.gfb.bean.DataMap;
import com.zfei.gfb.bean.Status;
import com.zfei.gfb.bean.StatusException;
import com.zfei.gfb.core.Auth;
import com.zfei.gfb.core.HttpJson;
import com.zfei.gfb.core.LogResult;
import com.zfei.gfb.dao.GfbLandPublicityMapper;
import com.zfei.gfb.device.AppContext;
import com.zfei.gfb.model.GfbLandPublicity;
import com.zfei.gfb.model.GfbLandResources;
import com.zfei.gfb.model.GfbUser;
import com.zfei.gfb.query.GfbLandPublicityQuery;
import com.zfei.gfb.request.GfbDelRequest;
import com.zfei.gfb.request.GfbGetRequest;
import com.zfei.gfb.request.GfbLandPublicityAddRequest;
import com.zfei.gfb.request.GfbLandPublicityEditRequest;
import com.zfei.gfb.util.Beans;
import com.zfei.gfb.util.Gsons;
import com.zfei.gfb.util.Now;
import com.zfei.gfb.util.gson.Types;
import com.zfei.gfb.vo.GfbJobWantedListVo;
import com.zfei.gfb.vo.GfbLandPublicityListVo;
import com.zfei.gfb.vo.GfbLandPublicityVo;
import com.zfei.gfb.vo.GfbLandResourcesVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
/**
 * @version 1.0
 * @author
 */
@Auth(type = {Const.VILLAGE_MANAGER, Const.TOWN_MANAGER})
@Api(tags = "土地，池塘，山林中标管理接口")
@RestController
@RequestMapping("/web/land/publicity")
public class GfbLandPublicityController  {

	@Resource
	private GfbLandPublicityMapper gfbLandPublicityMapper;

	@Resource
	private HttpSession httpSession;

	@Resource
	private StatsService statsService;

	@ApiOperation(value = "列表")
	@ApiResponses({
			@ApiResponse(code = 200, message = "", response = GfbLandPublicityListVo.class),
	})
	@LogResult
	@HttpJson("/list")
	public Object list(@RequestBody GfbLandPublicityQuery query) {
		query.check().build(Now::millis);

		GfbLandPublicityListVo result = new GfbLandPublicityListVo();

		int total = gfbLandPublicityMapper.total(query);
		result.setTotal(total);
		if (total == 0) {
			result.setData(Lists.newArrayList());
			return result;
		}

		List<GfbLandPublicity> list = gfbLandPublicityMapper.list(query);
		List<GfbLandPublicityVo> gfbLandPublicityVos  = Beans.copys(list,GfbLandPublicityVo.class);

		for (GfbLandPublicityVo vo : gfbLandPublicityVos) {

			switch (vo.getType()){
				case Const.LAND:
					vo.setBrowseNum(statsService.get(vo.getId().toString(), StatsTag.LAND_PUBLICITY_BROWSE_COUNT).getNum());
					break;
				case Const.POOL:
					vo.setBrowseNum(statsService.get(vo.getId().toString(), StatsTag.POND_PUBLICITY_BROWSE_COUNT).getNum());
					break;
				case Const.FOREST:
					vo.setBrowseNum(statsService.get(vo.getId().toString(), StatsTag.MOUNTAIN_PUBLICITY_BROWSE_COUNT).getNum());
					break;
				default:break;
			}
		}

		result.setData(gfbLandPublicityVos);
		return result;

	}

	@ApiOperation(value = "新增")
	@LogResult
	@HttpJson("/add")
	public Object add(@RequestBody GfbLandPublicityAddRequest request) {
		request.checkInsertNull();

		GfbLandPublicity record = Beans.copy(request, GfbLandPublicity.class);

		GfbUser user = (GfbUser) httpSession.getAttribute(AppContext.SESSION_WEB_USER);

		record.setUserId(user.getId());
		record.setCreateTime(Now.millis());
		record.setUpdateTime(Now.millis());
		record.setCntime(DateUtil.now());

		gfbLandPublicityMapper.insertSelective(record);
		return null;
	}

	@ApiOperation(value = "编辑")
	@LogResult
	@HttpJson("/edit")
	public Object edit(@RequestBody GfbLandPublicityEditRequest request) {
		request.checkUpdateNull();

		GfbLandPublicity record = gfbLandPublicityMapper.selectByPrimaryKey(request.getId());
		if (record == null) {
			throw new StatusException(Status.NOT_EXIST);
		}

		BeanUtils.copyProperties(request, record);
		record.setUpdateTime(Now.millis());

		gfbLandPublicityMapper.updateByPrimaryKeySelective(record);

		return null;
	}

	@ApiOperation(value = "删除")
	@LogResult
	@HttpJson("/del")
	public Object del(@RequestBody GfbDelRequest req) {
		req.checkDelNull();
		Integer id = req.getId();

		GfbLandPublicity record = gfbLandPublicityMapper.selectByPrimaryKey(id);
		if (record == null) {
			throw new StatusException(Status.NOT_EXIST);
		}

		gfbLandPublicityMapper.deleteByPrimaryKey(id);
		return null;
	}

	@ApiOperation(value = "土地，池塘，山林中标详情")
	@ApiResponses({
			@ApiResponse(code = 200, message = "",response = GfbLandPublicityVo.class),
	})
	@LogResult
	@HttpJson("/get")
	public Object get(@RequestBody GfbGetRequest request) {
		request.checkGetNull();
		Integer id = request.getId();

		GfbLandPublicity record = gfbLandPublicityMapper.selectByPrimaryKey(id);

		GfbLandPublicityVo vo = Beans.copy(record, GfbLandPublicityVo.class);

		return vo;
	}

}
